#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define CHUNK_SIZE 5120000000

int main() {
    FILE *file;
    char filename[] = "gemma-2b-it.ckpt"; // 模型名称
    char *file_content;
    clock_t start, end;
    double cpu_time_used;

    // 打开文件
    file = fopen(filename, "rb");
    if (file == NULL) {
        printf("no open %s.\n", filename);
        return 1;
    }

    // 计算文件大小
    fseek(file, 0, SEEK_END);
    long file_size = ftell(file);
    printf("file size:%ld B\n", file_size);
    rewind(file);

    // 分配内存以存储文件内容
    file_content = (char *)malloc((file_size) * sizeof(char));
    if (file_content == NULL) {
        printf("no mem.\n");
        return 1;
    }

    // 读取文件内容到内存中
    size_t total_bytes_read = 0;
    size_t bytes_read;
    while ((bytes_read = fread(file_content + total_bytes_read, 1, CHUNK_SIZE, file)) > 0) {
        total_bytes_read += bytes_read;
    }

    if (total_bytes_read != file_size) {
        printf("no read.\n");
        return 1;
    }

    // 关闭文件
    fclose(file);

    start = clock(); // 记录开始时间

    // 遍历文件内容
    printf("start...\n");
    printf("%c",file_content[file_size]);
    long i = 0;
    while (i <= file_size) {
        volatile char c = file_content[i];
        i++;
    }
    
    end = clock(); // 记录结束时间

    cpu_time_used = ((double)(end - start)) / CLOCKS_PER_SEC;
    printf("time: %f s\n", cpu_time_used);
    
    
    free(file_content);

    return 0;
}
